package com.eurake.seckill.Exception;

import com.eurake.seckill.vo.RespBean;
import com.eurake.seckill.vo.RespBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public RespBean handlerException(Exception e) {
        if(e instanceof GlobalException e1){
            return RespBean.error(e1.getRespBeanEnum());
        }else if(e instanceof BindException e1){
            RespBean respBean = RespBean.error(RespBeanEnum.ERROR);
            respBean.setMsg("参数校验异常:"+e1.getBindingResult().getAllErrors().getFirst().getDefaultMessage());
            return respBean;
        }
        return RespBean.error(e);
    }
}
